/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfoeducation.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.UpStudy;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.UpStudyService;
import com.lczyfz.campusinfo.common.utils.Const;
/**
 * 升学的业务Controller
 * @author zms
 * @version 2018-04-08
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfoeducation/upStudy")
public class UpStudyController extends BaseController {

	@Autowired
	private UpStudyService upStudyService;
	
	@ModelAttribute
	public UpStudy get(@RequestParam(required=false) String id) {
		UpStudy entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = upStudyService.get(id);
		}
		if (entity == null){
			entity = new UpStudy();
		}
		return entity;
	}
	
	@RequiresPermissions("campusinfoeducation:upStudy:view")
	@RequestMapping(value = {"list", ""})
	public String list(UpStudy upStudy, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<UpStudy> page = upStudyService.findPage(new Page<UpStudy>(request, response), upStudy); 
		model.addAttribute("page", page);
		return "modules/campusinfoeducation/upStudyList";
	}

	@RequiresPermissions("campusinfoeducation:upStudy:view")
	@RequestMapping(value = "form")
	public String form(UpStudy upStudy, Model model) {
		model.addAttribute("upStudy", upStudy);
		return "modules/campusinfoeducation/upStudyForm";
	}

	@RequiresPermissions("campusinfoeducation:upStudy:edit")
	@RequestMapping(value = "save")
	public String save(UpStudy upStudy, Model model,  HttpServletRequest request,RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, upStudy)){
			return form(upStudy, model);
		}
		if(StringUtils.isNotBlank(upStudy.getUpImg())) {
			upStudy.setUpImg(Const.getinetAddressService(upStudy.getUpImg(),request));
		}
		upStudyService.save(upStudy);
		addMessage(redirectAttributes, "保存升学成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfoeducation/upStudy/?repage";
	}
	
	@RequiresPermissions("campusinfoeducation:upStudy:edit")
	@RequestMapping(value = "delete")
	public String delete(UpStudy upStudy, RedirectAttributes redirectAttributes) {
		upStudyService.delete(upStudy);
		addMessage(redirectAttributes, "删除升学成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfoeducation/upStudy/?repage";
	}

}